package com.ilxqx.fierce.core;

import lombok.Data;
import org.springframework.util.ConcurrentReferenceHashMap;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * 动作方法持有器
 *
 * @author Venus
 * @since 1.0.0
 */
public final class ActionMethodHolder {

    /**
     * 处理方法缓存
     */
    private static final Map<ActionMethodCacheKey, ActionMethod> ACTION_METHOD_MAP = new ConcurrentReferenceHashMap<>(512);

    @Data
    private static class ActionMethodCacheKey {
        /**
         * 目标类
         */
        private final Object target;
        /**
         * 目标方法
         */
        private final Method method;
    }

    /**
     * 获取Action方法信息
     *
     * @param target 目标对象
     * @param targetClass 目标类
     * @param method 方法对象
     * @return Action方法信息
     */
    public static ActionMethod getActionMethod(Object target, Class<?> targetClass, Method method) {
        return ACTION_METHOD_MAP.computeIfAbsent(
            new ActionMethodCacheKey(target, method),
            key -> new ActionMethod(target, targetClass, method)
        );
    }
}
